﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Cricket.LinqModel;
using Cricket.LinqData;
using System.Web.Script.Services;

public partial class ClientBetting : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    BettingType bettingType;
    List<BettingType> bettingTypes;
    MatchType matchType;
    List<MatchType> matchTypes;
    CustomerBetting customerBetting;
    List<CustomerBetting> customerBettings = new List<CustomerBetting>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //if (Session["User"] != null)
            //    SessionManager.CurrentUser = (LoggedInUser)Session["User"];

            HttpCookie cookie = Request.Cookies["Client"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                bindMatchType();
                hdnAdminId.Value = Convert.ToString(cookie["AdminId"]);
            }

        }
    }

    private void bindMatch()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetTodayMatcheResult> lstResult = new List<GetTodayMatcheResult>();
        lstResult = matchSchedule.GetTodayMatches(Convert.ToInt32(drpMatchType.SelectedValue));
        if (lstResult != null && lstResult.Count > 0)
        {
            drpMatch.DataSource = lstResult;
            drpMatch.DataTextField = "Teams";
            drpMatch.DataValueField = "Id";
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
        else
        {
            drpMatch.DataSource = null;
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
    }
    private void bindBettingType()
    {
        bettingType = new BettingType(_dataObject);
        bettingTypes = new List<BettingType>();
        bettingTypes = bettingType.GetAllBettingType();
        if (bettingTypes.Count > 0)
        {
            drpBettingType.DataSource = bettingTypes;
            drpBettingType.DataTextField = "Type";
            drpBettingType.DataValueField = "Id";
            drpBettingType.DataBind();
            drpBettingType.Items.Insert(0, new ListItem("Select Betting Type", "0"));
        }
    }
    private void bindMatchType()
    {
        matchType = new MatchType(_dataObject);
        matchTypes = new List<MatchType>();
        matchTypes = matchType.GetAllMatchType();
        if (matchTypes.Count > 0)
        {
            drpMatchType.DataSource = matchTypes;
            drpMatchType.DataTextField = "Type";
            drpMatchType.DataValueField = "Id";
            drpMatchType.DataBind();
            drpMatchType.Items.Insert(0, new ListItem("Select Match Type", "0"));
        }
        else
        {
            drpMatchType.DataSource = null;
            drpMatchType.DataBind();
            drpMatchType.Items.Insert(0, new ListItem("Select Match Type", "0"));
        }
    }
    protected void drpMatchType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpMatchType.SelectedValue != "")
            bindMatch();
    }
    protected void drpMatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpMatch.SelectedItem.Text.ToLower() != "select match" && drpMatch.SelectedItem.Text.ToLower() != "select match type")
        {
            //matchSchedule = new MatchSchedule(_dataObject);
            //List<GetTodayMatcheResult> lstResult = new List<GetTodayMatcheResult>();
            //lstResult = matchSchedule.GetTodayMatches(Convert.ToInt32(drpMatchType.SelectedValue));

            matchSchedule = new MatchSchedule(_dataObject);
            List<GetMatchDetailsResult> lstResult = new List<GetMatchDetailsResult>();
            lstResult = matchSchedule.GetMatchDetails(Convert.ToInt32(drpMatch.SelectedValue));
            lblTeamA.Text = lstResult[0].Team1;
            lblTeamB.Text = lstResult[0].Team2;
            hdnTeamAId.Value = Convert.ToString(lstResult[0].TeamAId);
            hdnTeamBId.Value = Convert.ToString(lstResult[0].TeamBId);

            bindBettingType();
        }
    }
    protected void drpBettingType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpBettingType.SelectedValue != "")
        {
            switch (drpBettingType.SelectedValue)
            {
                case "1": //Session
                    divOverTeamA.Attributes.Add("style", "display:inline-block;");
                    divOverTeamB.Attributes.Add("style", "display:inline-block;");
                    divOverTie.Attributes.Add("style", "display:none;");
                    divOverDraw.Attributes.Add("style", "display:none;");

                    divOver.Attributes.Add("class", "input-prepend");
                    divLagadi.Attributes.Add("class", "input-prepend");
                    divKhadhi.Attributes.Add("class", "input-prepend");

                    spLagadi.InnerText = "Khadhi";
                    spKhadhi.InnerText = "Lagadi";

                    break;
                case "2": //Match
                    divOverTeamA.Attributes.Add("style", "display:none;");
                    divOverTeamB.Attributes.Add("style", "display:none;");
                    divOverTie.Attributes.Add("style", "display:none;");
                    divOverDraw.Attributes.Add("style", "display:none;");

                    divOver.Attributes.Add("class", "input-prepend hide");
                    divLagadi.Attributes.Add("class", "input-prepend");
                    divKhadhi.Attributes.Add("class", "input-prepend");

                    spLagadi.InnerText = "Lagadi";
                    spKhadhi.InnerText = "Khadhi";

                    break;
            }

            divBettingTeamA.Attributes.Add("style", "display:inline-block;");
            divBettingTeamB.Attributes.Add("style", "display:inline-block;");
            if (drpMatchType.SelectedItem.Text.ToLower() == "one day" || drpMatchType.SelectedItem.Text.ToLower() == "t20")
            {
                if (drpBettingType.SelectedItem.Text.ToLower() == "session")
                {
                    divBettingTie.Attributes.Add("style", "display:none;");
                    divBettingDraw.Attributes.Add("style", "display:none;");
                }
                else if (drpBettingType.SelectedItem.Text.ToLower() == "match")
                {
                    divBettingTie.Attributes.Add("style", "display:inline-block;");
                    divBettingDraw.Attributes.Add("style", "display:none;");
                }
            }
            else if (drpMatchType.SelectedItem.Text.ToLower() == "test")
            {
                if (drpBettingType.SelectedItem.Text.ToLower() == "session")
                {
                    divBettingTie.Attributes.Add("style", "display:none;");
                    divBettingDraw.Attributes.Add("style", "display:none;");
                }
                else if (drpBettingType.SelectedItem.Text.ToLower() == "match")
                {
                    divBettingTie.Attributes.Add("style", "display:inline-block;");
                    divBettingDraw.Attributes.Add("style", "display:inline-block;");
                }

            }

        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        HttpCookie cookie = Request.Cookies["Client"];
        if (cookie == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            //Team A
            customerBetting = new CustomerBetting(_dataObject);
            customerBetting.MatchSchduleId = Convert.ToInt32(drpMatch.SelectedValue);
            customerBetting.BettingId = TeamABettingId.Text == "" ? 0 : Convert.ToInt32(TeamABettingId.Text);
            customerBetting.Rate = txtSessionLagadiTeamA.Text.Trim().Equals("") ? txtSessionKhadhiTeamA.Text : txtSessionLagadiTeamA.Text;
            customerBetting.BetFor = txtSessionLagadiTeamA.Text.Trim().Equals("") ? "K" : "L";
            customerBetting.CreatedBy = Convert.ToInt32(cookie["ClientId"]);
            customerBetting.CreatedDate = System.DateTime.UtcNow;
            if (drpBettingType.SelectedItem.Text.ToLower().Equals("match") && customerBetting.BetFor != "" && customerBetting.Rate != "")
            {
                if (customerBetting.BetFor == "K")
                {
                    customerBetting.TeamAAmount = (Convert.ToDecimal(customerBetting.Rate) * Convert.ToDecimal(hdnTeamAK.Value)) * -1;
                    customerBetting.TeamBAmount = Convert.ToDecimal(customerBetting.Rate);
                    if (drpMatchType.SelectedItem.Text.ToLower() == "one day" || drpMatchType.SelectedItem.Text.ToLower() == "t20")
                    {
                        customerBetting.TieAmount = Convert.ToDecimal(customerBetting.Rate);
                    }
                    else if (drpMatchType.SelectedItem.Text.ToLower() == "test")
                    {
                        customerBetting.DrawAmount = Convert.ToDecimal(customerBetting.Rate);
                    }
                }
                else
                {
                    customerBetting.TeamAAmount = (Convert.ToDecimal(customerBetting.Rate) * Convert.ToDecimal(hdnTeamAL.Value));
                    customerBetting.TeamBAmount = Convert.ToDecimal(customerBetting.Rate) * -1;
                    if (drpMatchType.SelectedItem.Text.ToLower() == "one day" || drpMatchType.SelectedItem.Text.ToLower() == "t20")
                    {
                        customerBetting.TieAmount = Convert.ToDecimal(customerBetting.Rate) * -1;
                    }
                    else if (drpMatchType.SelectedItem.Text.ToLower() == "test")
                    {
                        customerBetting.DrawAmount = Convert.ToDecimal(customerBetting.Rate) * -1;
                    }
                }
            }
            //customerBetting.BettingAmount = Convert.ToDecimal(customerBetting.BetFor == "K" ? Convert.ToDouble(spSessionKhadhiTeamA.InnerHtml) : Convert.ToDouble(spSessionLagadiTeamA.InnerHtml) * Convert.ToDouble(customerBetting.Rate));

            if (!string.IsNullOrEmpty(customerBetting.Rate))
                customerBettings.Add(customerBetting);

            //Team B
            customerBetting = new CustomerBetting(_dataObject);
            customerBetting.MatchSchduleId = Convert.ToInt32(drpMatch.SelectedValue);
            customerBetting.BettingId = TeamBBettingId.Text == "" ? 0 : Convert.ToInt32(TeamBBettingId.Text);
            customerBetting.Rate = txtSessionLagadiTeamB.Text.Trim().Equals("") ? txtSessionKhadhiTeamB.Text : txtSessionLagadiTeamB.Text;
            customerBetting.BetFor = txtSessionLagadiTeamB.Text.Trim().Equals("") ? "K" : "L";
            customerBetting.CreatedBy = Convert.ToInt32(cookie["ClientId"]);
            customerBetting.CreatedDate = System.DateTime.UtcNow;
            if (drpBettingType.SelectedItem.Text.ToLower().Equals("match") && customerBetting.BetFor != "" && customerBetting.Rate != "")
            {
                if (customerBetting.BetFor == "K")
                {
                    customerBetting.TeamAAmount = Convert.ToDecimal(customerBetting.Rate);
                    customerBetting.TeamBAmount = (Convert.ToDecimal(customerBetting.Rate) * Convert.ToDecimal(hdnTeamBK.Value)) * -1;
                    if (drpMatchType.SelectedItem.Text.ToLower() == "one day" || drpMatchType.SelectedItem.Text.ToLower() == "t20")
                    {
                        customerBetting.TieAmount = Convert.ToDecimal(customerBetting.Rate);
                    }
                    else if (drpMatchType.SelectedItem.Text.ToLower() == "test")
                    {
                        customerBetting.DrawAmount = Convert.ToDecimal(customerBetting.Rate);
                    }
                }
                else
                {
                    customerBetting.TeamAAmount = Convert.ToDecimal(customerBetting.Rate);
                    customerBetting.TeamBAmount = (Convert.ToDecimal(customerBetting.Rate) * Convert.ToDecimal(hdnTeamBL.Value));
                    if (drpMatchType.SelectedItem.Text.ToLower() == "one day" || drpMatchType.SelectedItem.Text.ToLower() == "t20")
                    {
                        customerBetting.TieAmount = Convert.ToDecimal(customerBetting.Rate);
                    }
                    else if (drpMatchType.SelectedItem.Text.ToLower() == "test")
                    {
                        customerBetting.DrawAmount = Convert.ToDecimal(customerBetting.Rate);
                    }
                }
            }

            if (!string.IsNullOrEmpty(customerBetting.Rate))
                customerBettings.Add(customerBetting);

            //Team Tie
            customerBetting = new CustomerBetting(_dataObject);
            customerBetting.MatchSchduleId = Convert.ToInt32(drpMatch.SelectedValue);
            customerBetting.BettingId = TeamTieBettingId.Text == "" ? 0 : Convert.ToInt32(TeamTieBettingId.Text);
            customerBetting.Rate = txtSessionLagadiTeamTie.Text.Trim().Equals("") ? txtSessionKhadhiTeamTie.Text : txtSessionLagadiTeamTie.Text;
            customerBetting.BetFor = txtSessionLagadiTeamTie.Text.Trim().Equals("") ? "K" : "L";
            customerBetting.CreatedBy = Convert.ToInt32(cookie["ClientId"]);
            customerBetting.CreatedDate = System.DateTime.UtcNow;

            if (drpBettingType.SelectedItem.Text.ToLower().Equals("match") && customerBetting.BetFor != "" && customerBetting.Rate != "")
            {

            }
            //customerBetting.BettingAmount = Convert.ToDecimal(customerBetting.BetFor == "K" ? Convert.ToDouble(spSessionKhadhiTeamTie.InnerHtml) : Convert.ToDouble(spSessionLagadiTeamTie.InnerHtml) * Convert.ToDouble(customerBetting.Rate));

            if (!string.IsNullOrEmpty(customerBetting.Rate))
                customerBettings.Add(customerBetting);

            //Team Draw
            customerBetting = new CustomerBetting(_dataObject);
            customerBetting.MatchSchduleId = Convert.ToInt32(drpMatch.SelectedValue);
            customerBetting.BettingId = TeamDrawBettingId.Text == "" ? 0 : Convert.ToInt32(TeamDrawBettingId.Text);
            customerBetting.Rate = txtSessionLagadiTeamDraw.Text.Trim().Equals("") ? txtSessionKhadhiTeamDraw.Text : txtSessionLagadiTeamDraw.Text;
            customerBetting.BetFor = txtSessionLagadiTeamDraw.Text.Trim().Equals("") ? "K" : "L";
            customerBetting.CreatedBy = Convert.ToInt32(cookie["ClientId"]);
            customerBetting.CreatedDate = System.DateTime.UtcNow;

            if (drpBettingType.SelectedItem.Text.ToLower().Equals("match") && customerBetting.BetFor != "" && customerBetting.Rate != "")
                //customerBetting.BettingAmount = Convert.ToDecimal(customerBetting.BetFor == "K" ? Convert.ToDouble(spSessionKhadhiTeamDraw.InnerHtml) : Convert.ToDouble(spSessionLagadiTeamDraw.InnerHtml) * Convert.ToDouble(customerBetting.Rate));

                if (!string.IsNullOrEmpty(customerBetting.Rate))
                    customerBettings.Add(customerBetting);

            customerBetting.CreateCustomerBetting(customerBettings);

            #region Call the SP for Calculation

            #endregion

            layerSuccess.Attributes.Add("style", "display:block;");
            layerSuccess.InnerHtml = "Betting Added Successfully!";

            txtSessionLagadiTeamA.Text = string.Empty;
            txtSessionLagadiTeamB.Text = string.Empty;
            txtSessionLagadiTeamTie.Text = string.Empty;
            txtSessionLagadiTeamDraw.Text = string.Empty;

            txtSessionKhadhiTeamA.Text = string.Empty;
            txtSessionKhadhiTeamB.Text = string.Empty;
            txtSessionKhadhiTeamTie.Text = string.Empty;
            txtSessionKhadhiTeamDraw.Text = string.Empty;
        }
    }
    [WebMethod]

    public static List<GetSessionBettingForClientResult> GetBettingSessionRecords(int adminId, int matchId, int bettingType)
    {
        IDataRepository _dataObject1 = new DataRepository();
        CustomerBetting custBetting;

        custBetting = new CustomerBetting(_dataObject1);
        List<GetSessionBettingForClientResult> clientBettings = new List<GetSessionBettingForClientResult>();
        clientBettings = custBetting.GetSessionBettingForClient(adminId, matchId, bettingType);

        if (clientBettings != null && clientBettings.Count > 0)
        {
            List<GetSessionBettingForClientResult> OnResults = new List<GetSessionBettingForClientResult>();
            OnResults = clientBettings.ToList().Where(x => x.BettingOnOff.ToLower().Equals("on")).ToList();

            if (OnResults != null && OnResults.Count == 1)
            {
                OnResults.Add(clientBettings.ToList().Where(x => x.BettingOnOff.ToLower().Equals("off") && x.TeamName != OnResults.FirstOrDefault().TeamName).OrderByDescending(x => x.Id).FirstOrDefault());
            }
            else if (OnResults.Count == 0)
            {
                //Get Off Results.
                OnResults.AddRange(clientBettings.Where(x => x.BettingOnOff.ToLower().Equals("off")).OrderByDescending(x => x.Id).Take(2).ToList());
            }


            //if (OffResults != null)
            //OnResults.Add(OffResults);

            return OnResults;
        }
        else
            return null;
    }
}